package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineTakeDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineTakeDetailDAO;

import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.MedicineTake;
import cn.edu.scau.cmi.hombio.domain.MedicineTakeDetail;

import cn.edu.scau.cmi.hombio.service.MedicineTakeDetailService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for MedicineTakeDetail entities
 * 
 */

@Controller("MedicineTakeDetailRestController")

public class MedicineTakeDetailRestController {

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages MedicineTake entities
	 * 
	 */
	@Autowired
	private MedicineTakeDAO medicineTakeDAO;

	/**
	 * DAO injected by Spring that manages MedicineTakeDetail entities
	 * 
	 */
	@Autowired
	private MedicineTakeDetailDAO medicineTakeDetailDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for MedicineTakeDetail entities
	 * 
	 */
	@Autowired
	private MedicineTakeDetailService medicineTakeDetailService;

	/**
	 * View an existing Medicine entity
	 * 
	 */
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicine/{medicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public Medicine loadMedicineTakeDetailMedicine(@PathVariable Integer medicinetakedetail_id, @PathVariable Integer related_medicine_id) {
		Medicine medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		return medicine;
	}

	/**
	* View an existing MedicineTake entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicineTake/{medicinetake_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineTake loadMedicineTakeDetailMedicineTake(@PathVariable Integer medicinetakedetail_id, @PathVariable Integer related_medicinetake_id) {
		MedicineTake medicinetake = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetake_id, -1, -1);

		return medicinetake;
	}

	/**
	* Get Medicine entity by MedicineTakeDetail
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicine", method = RequestMethod.GET)
	@ResponseBody
	public Medicine getMedicineTakeDetailMedicine(@PathVariable Integer medicinetakedetail_id) {
		return medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail_id).getMedicine();
	}

	/**
	* Delete an existing MedicineTakeDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineTakeDetail(@PathVariable Integer medicinetakedetail_id) {
		MedicineTakeDetail medicinetakedetail = medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail_id);
		medicineTakeDetailService.deleteMedicineTakeDetail(medicinetakedetail);
	}

	/**
	* Show all MedicineTakeDetail entities
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicineTakeDetail> listMedicineTakeDetails() {
		return new java.util.ArrayList<MedicineTakeDetail>(medicineTakeDetailService.loadMedicineTakeDetails());
	}

	/**
	* Create a new MedicineTakeDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail", method = RequestMethod.POST)
	@ResponseBody
	public MedicineTakeDetail newMedicineTakeDetail(@RequestBody MedicineTakeDetail medicinetakedetail) {
		medicineTakeDetailService.saveMedicineTakeDetail(medicinetakedetail);
		return medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail.getId());
	}

	/**
	* Select an existing MedicineTakeDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineTakeDetail loadMedicineTakeDetail(@PathVariable Integer medicinetakedetail_id) {
		return medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail_id);
	}

	/**
	* Delete an existing MedicineTake entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicineTake/{medicinetake_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineTakeDetailMedicineTake(@PathVariable Integer medicinetakedetail_id, @PathVariable Integer related_medicinetake_id) {
		medicineTakeDetailService.deleteMedicineTakeDetailMedicineTake(medicinetakedetail_id, related_medicinetake_id);
	}

	/**
	* Create a new Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicine", method = RequestMethod.POST)
	@ResponseBody
	public Medicine newMedicineTakeDetailMedicine(@PathVariable Integer medicinetakedetail_id, @RequestBody Medicine medicine) {
		medicineTakeDetailService.saveMedicineTakeDetailMedicine(medicinetakedetail_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Create a new MedicineTake entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicineTake", method = RequestMethod.POST)
	@ResponseBody
	public MedicineTake newMedicineTakeDetailMedicineTake(@PathVariable Integer medicinetakedetail_id, @RequestBody MedicineTake medicinetake) {
		medicineTakeDetailService.saveMedicineTakeDetailMedicineTake(medicinetakedetail_id, medicinetake);
		return medicineTakeDAO.findMedicineTakeByPrimaryKey(medicinetake.getId());
	}

	/**
	* Save an existing MedicineTakeDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineTakeDetail saveMedicineTakeDetail(@RequestBody MedicineTakeDetail medicinetakedetail) {
		medicineTakeDetailService.saveMedicineTakeDetail(medicinetakedetail);
		return medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail.getId());
	}

	/**
	* Save an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicine", method = RequestMethod.PUT)
	@ResponseBody
	public Medicine saveMedicineTakeDetailMedicine(@PathVariable Integer medicinetakedetail_id, @RequestBody Medicine medicine) {
		medicineTakeDetailService.saveMedicineTakeDetailMedicine(medicinetakedetail_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Get MedicineTake entity by MedicineTakeDetail
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicineTake", method = RequestMethod.GET)
	@ResponseBody
	public MedicineTake getMedicineTakeDetailMedicineTake(@PathVariable Integer medicinetakedetail_id) {
		return medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail_id).getMedicineTake();
	}

	/**
	* Save an existing MedicineTake entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicineTake", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineTake saveMedicineTakeDetailMedicineTake(@PathVariable Integer medicinetakedetail_id, @RequestBody MedicineTake medicinetake) {
		medicineTakeDetailService.saveMedicineTakeDetailMedicineTake(medicinetakedetail_id, medicinetake);
		return medicineTakeDAO.findMedicineTakeByPrimaryKey(medicinetake.getId());
	}

	/**
	* Delete an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineTakeDetail/{medicinetakedetail_id}/medicine/{medicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineTakeDetailMedicine(@PathVariable Integer medicinetakedetail_id, @PathVariable Integer related_medicine_id) {
		medicineTakeDetailService.deleteMedicineTakeDetailMedicine(medicinetakedetail_id, related_medicine_id);
	}
}